package com.augurit.gzcgwsp.readAndParseXML.service.impl;

import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.augurit.common.util.lang.DateUtils;
import com.augurit.gzcgwsp.hwgz.service.IYwHwShSqbService;
import com.augurit.gzcgwsp.hwgz.web.form.YwHwShSqbForm;
import com.augurit.gzcgwsp.readAndParseXML.service.IHwggService;
import com.augurit.gzcgwsp.readAndParseXML.service.IParseXMLService;
import com.augurit.gzcgwsp.ywcmn.web.form.YwComQyxxForm;

/**
 * 解析户外广告xml数据
 * 
 * @author Administrator
 * 
 */

// 1.map里面的 会办部门 字段 还没有处理
// 2.yslNo 还没有加上去
@Service
@Transactional
public class HwggServiceImpl implements IHwggService {
	Logger logger = (Logger) LoggerFactory.getLogger(HwggServiceImpl.class);
	@Autowired
	private IParseXMLService parseXMLService;

	@Autowired
	private IYwHwShSqbService ywHwShSqbService;
	
	Locale locale = Locale.getDefault(); 
	ResourceBundle localResource = ResourceBundle.getBundle("interfaceExchange", locale); 

	// xml存放路径
	private final String hwShfilepath = localResource.getString("hwShfilepath");

	// 附件存放路径
	private final String hwShfujianpath = localResource.getString("hwShfujianpath");

	// 解析成功反馈xml存放路径
	private final String hwShfeedbackPath = localResource.getString("hwShfeedbackPath");

	// 解析异常xml存放路径
	private final String hwShexceptionPath = localResource.getString("hwShexceptionPath");

	// 解析后，xml备份
	private final String hwShbackPath = localResource.getString("hwShbackPath");

	private Map<String, String> hwShKeyMap;// xml内容的key

	private Map<String, String> hwShKeyMapHead;// xml头部的key

	private Map<String, Long> hwShfujianMap;// 附件的别名和id对应的map

	private String errorCode = "";// 异常编号

	private String errorStatus = "";// 异常描述

	private String errorStack = "";// 异常堆栈

	private void inithwShMap() {
		// 初始化数据部分对应的map
		hwShKeyMap = new HashMap<String, String>();
		
		hwShKeyMap.put("YSL_NO", "YSL_NO");
		hwShKeyMap.put("approvalNo", "approvalNo");
		hwShKeyMap.put("businessCode", "businessCode");
		hwShKeyMap.put("sendTime", "sendTime");
		
		hwShKeyMap.put("SQDW", "SQDW");
		hwShKeyMap.put("ADDRESS", "ADDRESS");
		hwShKeyMap.put("TYPE", "TYPE");
		hwShKeyMap.put("FR", "FR");
		hwShKeyMap.put("YYZZH", "YYZZH");
		hwShKeyMap.put("JBR", "JBR");
		hwShKeyMap.put("JBR_TEL", "JBR_TEL");
		hwShKeyMap.put("GGSZD", "GGSZD");
		hwShKeyMap.put("GD_NAME", "GD_NAME");
		hwShKeyMap.put("CDXZ", "CDXZ");
		hwShKeyMap.put("GGXZ", "GGXZ");
		hwShKeyMap.put("NSZQS_TIME", "NSZQS_TIME");
		hwShKeyMap.put("NSZZZ_TIME", "NSZZZ_TIME");
		hwShKeyMap.put("GGXS", "GGXS");
		hwShKeyMap.put("GGGGO_CD", "GGGGO_CD");
		hwShKeyMap.put("GGGGO_KD", "GGGGO_KD");
		hwShKeyMap.put("GGGGO_HD", "GGGGO_HD");
		hwShKeyMap.put("GGGGT_CD", "GGGGT_CD");
		hwShKeyMap.put("GGGGT_KD", "GGGGT_KD");
		hwShKeyMap.put("GGGGT_HD", "GGGGT_HD");
		hwShKeyMap.put("GGGGD_CD", "GGGGD_CD");
		hwShKeyMap.put("GGGGD_KD", "GGGGD_KD");
		hwShKeyMap.put("GGGGD_HD", "GGGGD_HD");
		hwShKeyMap.put("GGCL", "GGCL");
		hwShKeyMap.put("FRDBQZ", "FRDBQZ");
		hwShKeyMap.put("QZ_TIME", "QZ_TIME");
		hwShKeyMap.put("ACCEPT_NO", "ACCEPT_NO");
		hwShKeyMap.put("SJR", "SJR");
		hwShKeyMap.put("SARQ", "SARQ");
		hwShKeyMap.put("CBYW", "CBYW");
		hwShKeyMap.put("CBBM", "CBBM");
		// hwShKeyMap.put("会办部门", "会办部门");
		hwShKeyMap.put("CPSJ", "CPSJ");
		hwShKeyMap.put("DFSJ", "DFSJ");

		

		// 初始化头部对应的map
		hwShKeyMapHead = new HashMap<String, String>();
		hwShKeyMapHead.put("sn", "sn");
		hwShKeyMapHead.put("sender", "sender");
		// hwShKeyMapHead.put("receiver", "receiver");
		hwShKeyMapHead.put("businessCode", "businessCode");

		// 初始化附件的别名和id对应的map
		hwShfujianMap = new HashMap<String, Long>();
		hwShfujianMap.put("FJ_01", 95l);
		hwShfujianMap.put("FJ_02", 93l);
		hwShfujianMap.put("FJ_03", 94l);
		hwShfujianMap.put("FJ_04", 92l);
		hwShfujianMap.put("FJ_05", 96l);
		hwShfujianMap.put("FJ_06", 91l);
		hwShfujianMap.put("FJ_07", 510l);
		hwShfujianMap.put("FJ_08", 511l);
	}

	public void readXml(String projectPath) {
		if (hwShKeyMap == null)
			this.inithwShMap();
		File file = new File(hwShfilepath);
		File[] files = file.listFiles();
		if (files != null && files.length > 0) {

			for (int i = 0; i < files.length; i++) {
				if (files[i].isFile()) {
					String filePath = files[i].getPath();
					String fileName = files[i].getName();
					try {
						// 解析xml内容
						parseXMLService.initXmlFile(hwShKeyMap, filePath,hwShfeedbackPath, hwShexceptionPath);
						// 解析xml头部内容
						parseXMLService.initXmlFileHead(hwShKeyMapHead,files[i], hwShfeedbackPath, hwShexceptionPath);

						// 把数据部分保存到数据库
						YwHwShSqbForm form = new YwHwShSqbForm();
						for (String mapKey : hwShKeyMap.keySet()) {

							// TODO 把值放到form中
							form.setSqdw(parseXMLService.getMapValue(hwShKeyMap.get("SQDW")));
							
							YwComQyxxForm ywComQyxxForm = parseXMLService.getSqdwBySqdw(parseXMLService.getMapValue(hwShKeyMap.get("SQDW")),"hwggshsqb");
							if(ywComQyxxForm != null){
								form.setQyid(ywComQyxxForm.getId());
							}

							form.setYslNo(parseXMLService.getMapValue(hwShKeyMap.get("YSL_NO")));

							form.setAddress(parseXMLService.getMapValue(hwShKeyMap.get("ADDRESS")));
							form.setQylx(parseXMLService.getMapValue(hwShKeyMap.get("TYPE")));
							form.setFrxm(parseXMLService.getMapValue(hwShKeyMap.get("FR")));
							form.setZsbh(parseXMLService.getMapValue(hwShKeyMap.get("YYZZH")));
							form.setJbrxm(parseXMLService.getMapValue(hwShKeyMap.get("JBR")));
							form.setYwlxr(parseXMLService.getMapValue(hwShKeyMap.get("JBR")));
							form.setJbrtel(parseXMLService.getMapValue(hwShKeyMap.get("JBR_TEL")));
							form.setYwlxrtel(parseXMLService.getMapValue(hwShKeyMap.get("JBR_TEL")));
							form.setGgszd(parseXMLService.getMapValue(hwShKeyMap.get("GGSZD")));
							form.setGdName(parseXMLService.getMapValue(hwShKeyMap.get("GD_NAME")));
							form.setCdxz(parseXMLService.getMapValue(hwShKeyMap.get("CDXZ")));
							form.setGgxz(parseXMLService.getMapValue(hwShKeyMap.get("GGXZ")));

							String NSZQS_TIMETemp = parseXMLService.getMapValue(hwShKeyMap.get("NSZQS_TIME"));
							Date NSZQS_TIME = null;
							if (org.apache.commons.lang.StringUtils.isNotEmpty(NSZQS_TIMETemp)) {
								NSZQS_TIME = DateUtils.stringToDate(NSZQS_TIMETemp, DateUtils.YYYY_MM_DD);
								form.setNszqsTime(NSZQS_TIME);
							}

							String NSZZZ_TIMETemp = parseXMLService.getMapValue(hwShKeyMap.get("NSZZZ_TIME"));
							Date NSZZZ_TIME = null;
							if (org.apache.commons.lang.StringUtils.isNotEmpty(NSZZZ_TIMETemp)) {
								NSZZZ_TIME = DateUtils.stringToDate(NSZZZ_TIMETemp, DateUtils.YYYY_MM_DD);
								form.setNszzzTime(NSZZZ_TIME);
							}
							form.setGgxs(parseXMLService.getMapValue(hwShKeyMap.get("GGXS")));

							String GGGGO_CDTemp = parseXMLService.getMapValue(hwShKeyMap.get("GGGGO_CD"));
							Double GGGGO_CD = null;
							if (org.apache.commons.lang.StringUtils.isNotEmpty(GGGGO_CDTemp)) {
								GGGGO_CD = Double.parseDouble(GGGGO_CDTemp);
								form.setGgggoCd(GGGGO_CD);
							}

							String GGGGO_KDTemp = parseXMLService.getMapValue(hwShKeyMap.get("GGGGO_KD"));
							Double GGGGO_KD = null;
							if (org.apache.commons.lang.StringUtils.isNotEmpty(GGGGO_KDTemp)) {
								GGGGO_KD = Double.parseDouble(GGGGO_KDTemp);
								form.setGgggoKd(GGGGO_KD);
							}

							String GGGGO_HDTemp = parseXMLService.getMapValue(hwShKeyMap.get("GGGGO_HD"));
							Double GGGGO_HD = null;
							if (org.apache.commons.lang.StringUtils.isNotEmpty(GGGGO_HDTemp)) {
								GGGGO_HD = Double.parseDouble(GGGGO_HDTemp);
								form.setGgggoHd(GGGGO_HD);
							}

							String GGGGT_CDTemp = parseXMLService.getMapValue(hwShKeyMap.get("GGGGT_CD"));
							Double GGGGT_CD = null;
							if (org.apache.commons.lang.StringUtils.isNotEmpty(GGGGT_CDTemp)) {
								GGGGT_CD = Double.parseDouble(GGGGT_CDTemp);
								form.setGgggtCd(GGGGT_CD);
							}

							String GGGGT_KDTemp = parseXMLService.getMapValue(hwShKeyMap.get("GGGGT_KD"));
							Double GGGGT_KD = null;
							if (org.apache.commons.lang.StringUtils.isNotEmpty(GGGGT_KDTemp)) {
								GGGGT_KD = Double.parseDouble(GGGGT_KDTemp);
								form.setGgggtKd(GGGGT_KD);
							}

							String GGGGT_HDTemp = parseXMLService.getMapValue(hwShKeyMap.get("GGGGT_HD"));
							Double GGGGT_HD = null;
							if (org.apache.commons.lang.StringUtils.isNotEmpty(GGGGT_HDTemp)) {
								GGGGT_HD = Double.parseDouble(GGGGT_HDTemp);
								form.setGgggtHd(GGGGT_HD);
							}

							String GGGGD_CDTemp = parseXMLService.getMapValue(hwShKeyMap.get("GGGGD_CD"));
							Double GGGGD_CD = null;
							if (org.apache.commons.lang.StringUtils.isNotEmpty(GGGGD_CDTemp)) {
								GGGGD_CD = Double.parseDouble(GGGGD_CDTemp);
								form.setGgggdCd(GGGGD_CD);
							}

							String GGGGD_KDTemp = parseXMLService.getMapValue(hwShKeyMap.get("GGGGD_KD"));
							Double GGGGD_KD = null;
							if (org.apache.commons.lang.StringUtils.isNotEmpty(GGGGD_KDTemp)) {
								GGGGD_KD = Double.parseDouble(GGGGD_KDTemp);
								form.setGgggdKd(GGGGD_KD);
							}

							String GGGGD_HDTemp = parseXMLService.getMapValue(hwShKeyMap.get("GGGGD_HD"));
							Double GGGGD_HD = null;
							if (org.apache.commons.lang.StringUtils.isNotEmpty(GGGGD_HDTemp)) {
								GGGGD_HD = Double.parseDouble(GGGGD_HDTemp);
								form.setGgggdHd(GGGGD_HD);
							}
							form.setGgcl(parseXMLService.getMapValue(hwShKeyMap.get("GGCL")));
							String QZ_TIMETemp = parseXMLService.getMapValue(hwShKeyMap.get("QZ_TIME"));
							Date QZ_TIME = null;
							if (org.apache.commons.lang.StringUtils.isNotEmpty(QZ_TIMETemp)) {
								QZ_TIME = DateUtils.stringToDate(QZ_TIMETemp,DateUtils.YYYY_MM_DD);
							}
							form.setAcceptNo(parseXMLService.getMapValue(hwShKeyMap.get("ACCEPT_NO")));
							form.setSjr(parseXMLService.getMapValue(hwShKeyMap.get("SJR")));

							String SARQTemp = parseXMLService.getMapValue(hwShKeyMap.get("SARQ"));
							Date SARQ = new Date();
							if (org.apache.commons.lang.StringUtils.isNotEmpty(SARQTemp)) {
								SARQ = DateUtils.stringToDate(SARQTemp,DateUtils.YYYY_MM_DD);								
							}
							form.setSarq(SARQ);
							form.setCbyw(parseXMLService.getMapValue(hwShKeyMap.get("CBYW")));
							form.setCbbm(parseXMLService.getMapValue(hwShKeyMap.get("CBBM")));

							String CPSJTemp = parseXMLService.getMapValue(hwShKeyMap.get("CPSJ"));
							Date CPSJ = null;
							if (org.apache.commons.lang.StringUtils.isNotEmpty(CPSJTemp)) {
								CPSJ = DateUtils.stringToDate(CPSJTemp,DateUtils.YYYY_MM_DD);
								form.setCpsj(CPSJ);
							}

							String DFSJTemp = parseXMLService.getMapValue(hwShKeyMap.get("DFSJ"));
							Date DFSJ = null;
							if (org.apache.commons.lang.StringUtils.isNotEmpty(DFSJTemp)) {
								DFSJ = DateUtils.stringToDate(DFSJTemp,DateUtils.YYYY_MM_DD);
								form.setDfsj(DFSJ);
							}
							form.setApprovalNo(parseXMLService.getMapValue(hwShKeyMap.get("approvalNo")));
							
							form.setBusinessCode(parseXMLService.getMapValue(hwShKeyMap.get("businessCode")));
							String wstjsjStr = parseXMLService.getMapValue(hwShKeyMap.get("sendTime"));
							Date wstjsj = null;
							if (StringUtils.isNotEmpty(wstjsjStr)) {
								wstjsj = DateUtils.stringToDate(wstjsjStr, DateUtils.YYYY_MM_DD_HH_MM_SS);
								form.setWstjsj(wstjsj);
							}
							form.setSqrq(new Date());

							form.setSendNotice("Y");
							form.setYsl(1l);
						}
						// 调用DAO，保存到数据库
						try {
							ywHwShSqbService.save(form);
						} catch (Exception e) {
							// TODO 生成异常xml
							e.printStackTrace();
							errorCode = "1001";
							errorStatus = "插入数据库失败";
							parseXMLService.createSaveOrParseExceptionXml(errorCode, errorStatus, e, files, 
									hwShKeyMapHead, hwShfeedbackPath, hwShexceptionPath, i);
						}						

						//处理附件
						parseXMLService.handleFj(projectPath, fileName, hwShfujianpath, hwShfeedbackPath, hwShexceptionPath, hwShKeyMapHead,
								files, hwShfujianMap, i, form.getQyid(), form.getId(), "hwggshsqb");
					} catch (Exception e) {
						// TODO 生成异常xml
						e.printStackTrace();
						errorCode = "1000";
						errorStatus = "解析xml失败";
						parseXMLService.createSaveOrParseExceptionXml(errorCode, errorStatus, e, files, 
								hwShKeyMapHead, hwShfeedbackPath, hwShexceptionPath, i);
					}
					// 剪切文件,把文件移走
					parseXMLService.removeXML(filePath, hwShbackPath + fileName,
							fileName);
				}
			}
		}
	}

}
